package st.business.tepth.business.visitor.visitorwork.work.detail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.BindView;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.business.visitor.visitorwork.entity.WorkDetailBean;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.base.mvp.MvpFragment;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-06-05 17:33
 */

public class DetailDelegate extends MvpFragment<DetailPresenter>
        implements DetailContract.View {
    public static DetailDelegate newInstance(int repairTaskId) {
        Bundle bundle = new Bundle();
        bundle.putInt("repairTaskId", repairTaskId);
        DetailDelegate delegate = new DetailDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R2.id.layout_container)
    ConstraintLayout mLayoutContainer;
    @BindView(R2.id.layout_check)
    ViewGroup mLayoutCheck;
    @BindView(R2.id.layout_pass)
    ViewGroup mLayoutPass;
    @BindView(R2.id.layout_complete)
    ViewGroup mLayoutComplete;
    @BindView(R2.id.tv_pass_person)
    TextView mTvPassPerson;
    @BindView(R2.id.tv_pass_result)
    TextView mTvPassResult;
    @BindView(R2.id.tv_finish_person)
    TextView mTvFinishPerson;
    @BindView(R2.id.tv_finish_result)
    TextView mTvFinishResult;
    @BindView(R2.id.radio_group)
    RadioGroup mRadioGroup;
    @BindView(R2.id.rb_maintain)
    RadioButton mRbMaintain;
    @BindView(R2.id.rb_repair)
    RadioButton mRbRepair;
    @BindView(R2.id.tv_person)
    TextView mTvPerson;
    private int mRepairTaskId;
    private MaintainDelegate mMaintainDelegate;
    private RepairDelegate mRepairDelegate;
    private int mCheckId;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mRepairTaskId = bundle.getInt("repairTaskId");
        }
    }

    @Override
    public DetailPresenter getPresenter() {
        return new DetailPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_work_read_only_detail;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (mRepairDelegate != null && mMaintainDelegate != null) {
                    if (checkedId == R.id.rb_repair) {
                        showHideFragment(mRepairDelegate, mMaintainDelegate);
                    } else {
                        showHideFragment(mMaintainDelegate, mRepairDelegate);
                    }
                }
                mCheckId = checkedId;
            }
        });
        mCheckId = R.id.rb_maintain;
        mPresenter.getWorkDetail(UserManager.getInstance().getToken(), mRepairTaskId
        );
        addTopView(R.layout.view_loading, mLayoutContainer);
    }

    @Override
    public void showWorkDetail(WorkDetailBean workDetailBean) {
        removeTopView(mLayoutContainer);
        if (workDetailBean != null) {
            mTvPerson.setText(workDetailBean.getWork());
            WorkDetailBean.PassBean passBean = workDetailBean.getPass();
            WorkDetailBean.FinishBean finishBean = workDetailBean.getFinish();
            ArrayList<WorkDetailBean.MaintainBean> maintainBeans = workDetailBean.getMaintain();
            ArrayList<WorkDetailBean.RepairBean> repairBeans = workDetailBean.getRepair();
            if (passBean == null) {
                mLayoutPass.setVisibility(View.GONE);
            } else {
                mLayoutPass.setVisibility(View.VISIBLE);
                mTvPassPerson.setText(passBean.getPerson());
                mTvPassResult.setText(passBean.getState());
            }
            if (finishBean == null) {
                mLayoutComplete.setVisibility(View.GONE);
            } else {
                mLayoutComplete.setVisibility(View.VISIBLE);
                mTvFinishPerson.setText(finishBean.getPerson());
                mTvFinishResult.setText(finishBean.getState());
            }
            mMaintainDelegate = findChildFragment(MaintainDelegate.class);
            mRepairDelegate = findChildFragment(RepairDelegate.class);
            if (mMaintainDelegate == null || mRepairDelegate == null) {
                mMaintainDelegate = MaintainDelegate.newInstance(maintainBeans);
                mRepairDelegate = RepairDelegate.newInstance(repairBeans);
                loadMultipleRootFragment(R.id.layout_content, 0,
                        mMaintainDelegate,
                        mRepairDelegate);
            } else {
                mMaintainDelegate.resetData(maintainBeans);
                mRepairDelegate.resetData(repairBeans);
            }
        } else {
            addTopView(R.layout.view_search_empty, mLayoutContainer);
        }
    }
}